package cn.sdormitory.exam.service.impl;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.utils.SysUserUtils;
import cn.sdormitory.exam.dao.ClassroomTestDao;
import cn.sdormitory.exam.dao.ClassroomTestRecordDao;
import cn.sdormitory.exam.entity.ClassroomTest;
import cn.sdormitory.exam.entity.ClassroomTestRecord;
import cn.sdormitory.exam.service.ClassroomTestService;
import cn.sdormitory.exam.vo.ClassroomTestRecordVo;
import cn.sdormitory.exam.vo.ClassroomTestVo;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author : yy
 * @date: 2024/12/31 11:30
 * @description:
 */
@Service
public class ClassroomTestServiceImpl extends ServiceImpl<ClassroomTestDao, ClassroomTest> implements ClassroomTestService {
    @Resource
    private ClassroomTestDao classroomTestDao;
    @Resource
    private ClassroomTestRecordDao classroomTestRecordDao;


    @Override
    public CommonPage<ClassroomTestVo> getList(String testName, Integer courseId, Integer classId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ClassroomTestVo> list = classroomTestDao.getList(testName,courseId,classId);
        return CommonPage.restPage(list);
    }

    @Override
    public void delTest(Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        classroomTestDao.deleteBatchIds(idList);
    }

    @Override
    public ClassroomTest getTest(Integer id) {
        return classroomTestDao.selectById(id);
    }

    @Override
    public void editTest(ClassroomTest classroomTest) {
        SysUser user = SysUserUtils.getSysUser();
        if(classroomTest.getId()!=null) {
            classroomTest.setModifyBy(user.getUsername());
            classroomTestDao.updateById(classroomTest);
        }else {
            classroomTest.setCreateBy(user.getUsername());
            classroomTest.setModifyBy(user.getUsername());
            classroomTestDao.insert(classroomTest);
        }
    }

    @Override
    public List<ClassroomTestRecordVo> getClassroomTestRecord(Integer classId, Integer testId) {
        return classroomTestDao.getClassroomTestRecord(classId,testId);
    }

    @Override
    public void updateTestRecord(Integer testId, List<ClassroomTestRecordVo> recordVos) {
        LambdaQueryWrapper<ClassroomTestRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassroomTestRecord::getClassroomTestId,testId);
        classroomTestRecordDao.delete(wrapper);

        for(ClassroomTestRecordVo recordVo : recordVos) {
            ClassroomTestRecord record = new ClassroomTestRecord();
            BeanUtils.copyProperties(recordVo,record);
            record.setClassroomTestId(testId);
            classroomTestRecordDao.insert(record);
        }
    }
}
